package com.cat.dynamicPlanning;

import java.util.Arrays;
import java.util.List;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/length-of-the-longest-subsequence-that-sums-to-target/
 * @create 2025/9/1 19:21
 * @since JDK17
 */

public class Solution44 {
    public int lengthOfLongestSubsequence(List<Integer> nums, int target) {
        int n = nums.size();
        int[] dp = new int[target + 1];
        Arrays.fill(dp, -1);
        dp[0] = 0;
        for (int i = 0; i < n; i++) {
            for (int j = target; j >= nums.get(i); j--) {
                if (dp[j - nums.get(i)] != -1) {
                    dp[j] = Math.max(dp[j - nums.get(i)] + 1, dp[j]);
                }
            }
        }
        return dp[target];
    }
}
